<?php
namespace Admin\Controller;
use Think\Page;
class GoodsController extends CommonController {
    public function lst(){
		
		$goods=D('goodsView');
		$count=$goods->count();//查询满足要求的总纪录数
		$Page=new \Think\Page($count,10);//实例化分页类
		$show=$Page->show();
		
		$list = $goods->limit($Page->firstRow.','.$Page->listRows)->select();		
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	
	public function add(){
		$mod=D('goods');
		if(IS_POST){
			//处理从表单传过来的图片并生成三张缩略图
			if($_FILES['original'][tmp_name]){
				 $upload = new \Think\Upload();// 实例化上传类
				 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
				 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
				 $upload->savePath  =      './Public/Uploads/Goods/'; // 设置附件上传目录
				  $upload->rootPath  =      './'; // 设置附件上传目录
				 // $upload->autoSub   =     false;// 设置图片上传不生成子目录
				  $info   =   $upload->uploadOne($_FILES['original']); 
				  if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				  }else{
				 $original=$info['rootPath'] . $info['savepath'] . $info['savename']; 
				 $max_thumb=$info['rootPath'] . $info['savepath'] . 'max'.$info['savename']; 
				 $mid_thumb=$info['rootPath'].$info['savepath'].'mid_'.$info['savename'];
				 $sm_thumb=$info['rootPath'].$info['savepath'].'sm_'.$info['savename'];
				 $_POST['original']=$original;
				 $image = new \Think\Image();
				 $srcimg=$upload->rootPath.$original;
				 $image->open($srcimg);// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
				 $image->thumb(362, 362)->save($max_thumb);
				 $image->thumb(222, 222)->save($mid_thumb);
				 $image->thumb(67, 67)->save($sm_thumb);
				 $_POST['original']=$original;
				 $_POST['max_thumb']= $max_thumb;
				 $_POST['mid_thumb']=$mid_thumb;
				 $_POST['sm_thumb']=$sm_thumb;
				 $_POST['goods_sn']=time().rand(111111,999999);
				  }	
			}
				if($_POST['onsale']){
					 $_POST['onsale']=1;
				 }else{
					 $_POST['onsale']=0;
				 }
				 $_POST['goods_sn']=time().rand(111111,999999);
				 
			if($mod->create()){
				if($mod->add()){
						$this->success("添加商品成功！",U("lst"));
				}else{
						$this->error("添加商品失败！");
				}
			}else{
					$this->error($mod->getError());
			}	
			return;
		}
		
		$cate=D('cate');
		$cateres = $cate->catetree();			//调用catetree()进行无极排序，
		
		$brand=D('brand');//实例化品牌表
		$brandres = $brand->select();
				
		$memberLevel=D('memberLevel');//实例化会员等级表
		$levres = $memberLevel->select();
	
		$typeres=D('type')->select();//实例化商品类型表
		$recpos=D('recpos')->where(array('rectype'=>1))->select();
		
		$this->assign(array(
						'cateres'=>$cateres,
						'brandres'=>$brandres,
						'levres'=>$levres,
						'typeres'=>$typeres,
						'recpos'=>$recpos
						));
		$this->display();
	}
	
	
	
	
	public function edit(){
		$mod=D('goods');
		if(IS_POST){
			//dump($_POST);die;
			
			//处理从修改表单传过来的图片并生成三张缩略图
			if($_FILES['original'][tmp_name]){
				 $upload = new \Think\Upload();// 实例化上传类
				 $upload->maxSize   =     3145728 ;// 设置附件上传大小 
				 $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
				 $upload->savePath  =      './Public/Uploads/Goods/'; // 设置附件上传目录
				  $upload->rootPath  =      './'; // 设置附件上传目录
				 // $upload->autoSub   =     false;// 设置图片上传不生成子目录
				  $info   =   $upload->uploadOne($_FILES['original']); 
				  if(!$info) {// 上传错误提示错误信息
				//	$mod->error($upload->getError());
				  }else{
				 $original=$info['rootPath'] . $info['savepath'] . $info['savename']; 
				 $max_thumb=$info['rootPath'] . $info['savepath'] . 'max'.$info['savename']; 
				 $mid_thumb=$info['rootPath'].$info['savepath'].'mid_'.$info['savename'];
				 $sm_thumb=$info['rootPath'].$info['savepath'].'sm_'.$info['savename'];
				 $_POST['original']=$original;
				 $image = new \Think\Image();
				 $srcimg=$upload->rootPath.$original;
				 $image->open($srcimg);// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
				 $image->thumb(362, 362)->save($max_thumb);
				 $image->thumb(222, 222)->save($mid_thumb);
				 $image->thumb(67, 67)->save($sm_thumb);
				 $_POST['original']=$original;
				 $_POST['max_thumb']= $max_thumb;
				 $_POST['mid_thumb']=$mid_thumb;
				 $_POST['sm_thumb']=$sm_thumb;
				}
			}
			
			
			if($mod->create()){
				if($mod->save()!==false){
					$this->success("修改商品信息成功！",U('lst'));
				}else{
					$this->error("修改商品信息失败！");
				}
			}else{
				$mod->error($mod->getError());
			}
			return;
		}
		$id=I('id');
		$goods = $mod->find(I('id'));//查询从lst页面传递过来id号的数据；
		$cateres = D('cate')->catetree();			//调用catetree()进行无极排序，
		$brandres=D('brand')->select();
		$memberLevel=D('memberLevel');//实例化会员等级表
		$levres = $memberLevel->select();//
		$memberPrice=D('member_price')->where(array('goods_id'=>I('id')))->select();//查询商品会员价格表，并分配到模板
		$goodsPic=D('goodsPic')->where(array('goods_id'=>I('id')))->select();//实例化商品图片表
		$typeres=D('type')->select();//实例化商品类型表
		$attrres=D('attr')->where(array('type_id'=>$goods['type_id']))->select();//当前类型的所有属性
		$_gattrres=D('goodsAttr')->where(array('goods_id'=>$id))->select();//当前商品的所有属性
		$recpos=D('recpos')->where(array('rectype'=>1))->select();//查询当前商品的推荐位属性
		$_recvalues=D('recvalue')->field('recid')->where(array('rectype'=>1,'valueid'=>$id))->select();
		//将查询到的商品推荐位进行遍历，并存放到一维数组中
		$recids=array();
		foreach($_recvalues as $k=>$v){
			$recids[]=$v['recid'];
		}
		$gattrres=array();
		foreach($_gattrres as $k=>$v){
			$gattrres[$v['attr_id']][]=$v;
		}
		//dump($gattrres);die;
		$this->assign(array(
										'goods'=>$goods,
										'cateres'=>$cateres,
										'brandres'=>$brandres,
										'levres'=>$levres,
										'memberPrice'=>$memberPrice,
										'goodsPic'=>$goodsPic,
										'typeres'=>$typeres,
										'attrres'=>$attrres,//当前类型的所有属性
										'gattrres'=>$gattrres,//当前商品的所有属性
										'recpos'=>$recpos,
										'recids'=>$recids,//当前商品所属的推荐位
										));
		$this->display();
	}
	 
	 //对商品进行删除操作并同时删除商品的图片，会员价格，商品的属性
	public function del(){
		$id=I('id');
			$mod=D('goods');
			$res=$mod->field('original,max_thumb,mid_thumb,sm_thumb')->find(I('id'));
			$original='./'.$res['original'];
			$max_thumb='./'.$res['max_thumb'];
			$mid_thumb='./'.$res['mid_thumb'];
			$sm_thumb='./'.$res['sm_thumb'];
			//处理主图缩略图
			@unlink($original);
			@unlink($max_thumb);
			@unlink($mid_thumb);
			@unlink($sm_thumb);
		//删除会员价格
			D('memberPrice')->where(array('goods_id'=>$id))->delete();
			
			//删除商品的属性；
			D('goodsAttr')->where(array('goods_id'=>$id))->delete();
			
			$goodspicres=D('goodsPic')->where(array('goods_id'=>$id))->select();
			foreach($goodspicres as $k=>$v){
				
				$original='./'.$v['original'];
		
				$max_thumb='./'.$v['max_thumb'];
				
				$sm_thumb='./'.$v['sm_thumb'];
				@unlink($original);
				
				@unlink($max_thumb);
				
				@unlink($sm_thumb);
			}
			
			D('GoodsPic')->where(array('goods_id'=>$id))->delete();
			if(D('goods')->delete(I('id'))){
			$this->success('删除商品成功',U('lst'));
		}else{
			$this->error('删除商品失败111111111！');
		}
			
		}
		
		
		//处理商品的库存方法
public function product($id){
	$pro=D('product');
	if(IS_POST){
		//得到表单传递过来的添加商品属性库存，并存放到数据库
		$goods_num=I('goods_num');
		$goods_attr=I('goods_attr');
		$pro->where(array('goods_id'=>$id))->delete();
		foreach($goods_num as $k=>$v){
			$_attr=array();
			foreach($goods_attr  as  $k1=>$v1){
				
				if((int)$v1[$k]<=0){
					continue 2;
				}
				$_attr[]=$v1[$k];
			}
			sort($_attr);
			
			$goodsAttrstr=implode(',',$_attr);
			$pro->add(array(
							'goods_id'=>$id,
							'goods_number'=>$v,
							'goods_attr'=>$goodsAttrstr
			));
		}
		$this->success("添加库存成功！");
		return;
	}
	//获得商品的单选属性并将结果分配到模板展示
	$goods=D('goods');
	$_radioAttr=$goods->getRadioAttr($id);
	$radioAttr=array();
	foreach($_radioAttr as $k=>$v){
		$radioAttr[$v['attr_id']][]=$v;
	}
	$prores=$pro->where(array('goods_id'=>$id))->order('id desc')->select();
	$this->assign(array(
											'radioAttr'=>$radioAttr,
											'prores'=>$prores
											));
	
	$this->assign('radioAttr',$radioAttr);
	$this->display();
	
}
		
	
	//商品添加中判断是否有传图片
	public function hasImg($files){
			foreach($files as $k=>$vo){
				if($vo){
					return true;
				}
			}
			return false;
		}
		
		public function ajaxgetattr($typeid){
			$data=D('attr')->where(array('type_id'=>$typeid))->select();
			echo json_encode($data);
		}
		
		//处理异步删除图片
		public function ajaxdelpic($picid){
			$goodspic=D('goods_pic');
			$goodspic->find($picid);
			@unlink($goodspic->original);
			@unlink($goodspic->max_thumb);
			@unlink($goodspic->sm_thumb);
			$goodspic->delete();
			
		}
		
		public function ajaxdelga($gaid){
			D('GoodsAttr')->delete($gaid);
		}
	
}